package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-22
 * @Description: 供应商主数据新增申请流程调用cwssp041供应商生成修改接口
 * @ModifyLog: OA-T100，OA将供应商信息传给T100，T100将供应商编号回传给OA；
 **/
public class WeaGyszsjzxsqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaGyszsjzxsqlcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();
                //交易对象类型	1:供应商			默认：1
                masterJson.put("pmaa002", "1");
                //法人类型		默认：1
                masterJson.put("pmaa004", "1");
                String NAME_ORG1 = Util.null2String(rs.getString("NAME_ORG1"));
                //全称		供应商名称
                masterJson.put("pmaal003", NAME_ORG1);
                //简称		供应商名称
                masterJson.put("pmaal004", NAME_ORG1);
                //结账方式	1.依周期结算,2.依交易逐单结			默认：1
                masterJson.put("pmaa037","1" );
                //供应商经营类型	1.一般,2.经销商,3.代理商			默认：1
                masterJson.put("pmaa082", "1");
                //负责采购人员	采购员
                masterJson.put("pmab031", Util.null2String(rs.getString("cgy")));
                //交易币别	为供应商或两者皆是时不可为空	币种	WAERS	传编码
                masterJson.put("pmab033", Util.null2String(rs.getString("WAERS")));
                //交易税别	为供应商或两者皆是时不可为空			默认：Y13
                masterJson.put("pmab034", "Y13");
                //收款条件	付款条件
                masterJson.put("pmab037", Util.null2String(rs.getString("ZTERM")));
                //交易条件	默认：CRF
                masterJson.put("pmab053", Util.null2String(rs.getString("CRF")));
                //供应商联络人	销售人员
                masterJson.put("pmab075", Util.null2String(rs.getString("VERKF")));
                //备注		备注说明
                masterJson.put("ooff013", Util.null2String(rs.getString("bzsmnew")));

                JSONArray addressDetailList = new JSONArray();
                JSONObject addressDetailJson = new JSONObject();
                //地址类型	默认：5
                addressDetailJson.put("oofb008", "5");
                //国家/地区别		国家
                addressDetailJson.put("oofb012", Util.null2String(rs.getString("LAND1")));
                //邮政编号		邮政编码
                addressDetailJson.put("oofb013", Util.null2String(rs.getString("PSTLZ")));
                //地址		街道	STRAS
                addressDetailJson.put("oofb017", Util.null2String(rs.getString("STRAS")));
                addressDetailList.add(addressDetailJson);
                masterJson.put("address_detail", addressDetailList);

                JSONArray communicationDetailList = new JSONArray();
                JSONObject communicationDetailJson = new JSONObject();
                 //通信类型				默认：1
                communicationDetailJson.put("oofc008","1");
                //通信内容		电话
                communicationDetailJson.put("oofc012",Util.null2String(rs.getString("TELF1")));
                communicationDetailList.add(communicationDetailJson);
                masterJson.put("communication_detail", communicationDetailList);

                JSONArray pmadDetailList = new JSONArray();
                JSONObject pmadDetailJson = new JSONObject();
                //收款条件		付款条件
                pmadDetailJson.put("pmad002", Util.null2String(rs.getString("ZTERM")));
                //交易类型	1:付款 2:收款
                pmadDetailJson.put("pmad003", "1");
                pmadDetailList.add(pmadDetailJson);
                masterJson.put("pmad_detail", pmadDetailList);

                JSONArray bankDetailList = new JSONArray();
                JSONObject bankDetailJson = new JSONObject();
                //银行编号	开户行
                masterJson.put("pmaf002", Util.null2String(rs.getString("khh")));
                //银行账户	账号
                masterJson.put("pmaf003", Util.null2String(rs.getString("BANKN")));
                //账户名称		账户持有人
                masterJson.put("pmaf004", Util.null2String(rs.getString("KOINH")));
                //SWIFT CODE		税号
                masterJson.put("pmaf005", Util.null2String(rs.getString("STCD5")));
                //开户行名称		银行名称
                masterJson.put("pmaf012", Util.null2String(rs.getString("bzsm")));
                bankDetailList.add(bankDetailJson);
                masterJson.put("bank_detail", bankDetailList);

                JSONArray pmacDetailList = new JSONArray();
                JSONObject pmacDetailJson = new JSONObject();
                //交易类型		默认：1
                pmacDetailJson.put("pmac003", "1");
                //主要否			默认：Y
                pmacDetailJson.put("pmac004", "Y");
                pmacDetailList.add(pmacDetailJson);
                masterJson.put("pmac_detail", pmacDetailList);

                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaGyszsjzxsqlcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.ClentSupplierCreat(payloadJson, "WeaGyszsjzxsqlcAction", requestid, src);
                writeLog(">>>>>WeaGyszsjzxsqlcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set PARTNER='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaGyszsjzxsqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}